//
//  RecordDetailViewController.m
//  YunGui
//
//  Created by wmm on 16/6/3.
//  Copyright © 2016年 hanen. All rights reserved.
//

#import "RecordDetailViewController.h"
#import "SJAvatarBrowser.h"
#import "ofSeeImageView.h"


@interface RecordDetailViewController ()<UINavigationControllerDelegate>
{
    UIScrollView * scrollView;
    
    NSMutableArray  *_choosePicDataArray;
    NSMutableArray  *_cameraPicArray;
    NSMutableArray *pic;

}

@property(nonatomic,strong)UITextField  *timeTF;
@property(nonatomic,strong)UITextField  *nameTF;
@property(nonatomic,strong)UITextField  *telTF;
@property(nonatomic,strong)UITextView  *visitTV;
@property (strong, nonatomic) UIView *picView;
@property (strong, nonatomic) UIImageView *visitPic1;
@property (strong, nonatomic) UIImageView *visitPic2;
@property (strong, nonatomic) UIImageView *visitPic3;

@end

@implementation RecordDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = GrayBackColor;
    self.navigationItem.hidesBackButton = YES;
    [ViewTool setNavigationBar:self.navigationController.navigationBar];
    self.navigationItem.title = @"记录详情";
    self.navigationItem.leftBarButtonItem = [ViewTool getBarButtonItemWithTarget:self WithString:@"back" WithAction:@selector(goToBack)];
    
    scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64)];
    scrollView.showsVerticalScrollIndicator=NO;
    [self.view addSubview:scrollView];
    
    [self initListView];
}

- (void)goToBack{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)initListView{
    CGFloat startX = [UIView getWidth:20];
    CGFloat imgW = 20;
    CGFloat labX = startX + imgW +15;
    CGFloat labH = 40;
    CGFloat labY = 10;
    CGFloat height = labH - 2*labY;
    CGFloat grayH = 15;
    CGFloat labW  = 80;
    CGFloat tfX = labX + labW +[UIView getWidth:5];
    CGFloat tfW = kScreenWidth - tfX;
    CGFloat picSize = [UIView getWidth:40.0f];
    
    UIView *sectionView = [ViewTool getLineViewWith:CGRectMake(0, 0, kScreenWidth, grayH) withBackgroudColor:GrayBackColor];
    [scrollView addSubview:sectionView];
    UIView *line = [self getLineWith:CGRectMake(0, sectionView.maxY - 1, kScreenWidth, 1)];
    [scrollView addSubview:line];
    
    UIView *bg = [[UIView alloc] initWithFrame:CGRectMake(0, sectionView.maxY, kScreenWidth, 200)];
    bg.backgroundColor = [UIColor whiteColor];
    [scrollView addSubview:bg];
    
    UIImageView *recordImg = [self imageWith:CGRectMake(startX, line.maxY + labY +1.5, imgW, imgW) withName:@"Btn_Normal_Baifangjilu.png"];
    [scrollView addSubview:recordImg];
    
    UILabel *infoLabel = [self getLabelWith:CGRectMake(labX, line.maxY + labY +1.5, labW, height) WithTitle:@"拜访信息" withColor:BLACKCOLOR];
    infoLabel.font = [ViewTool getFont:16.0f];
    [scrollView addSubview:infoLabel];
    
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(infoLabel.x, infoLabel.maxY + labY+1.5 -1, kScreenWidth - infoLabel.x, 1)];
    lineView.backgroundColor = GRAYLINECOLOR;
    [scrollView addSubview:lineView];
    
    UILabel *timeLabel = [self getLabelWith:CGRectMake(labX, infoLabel.maxY + labY*2 , labW, height) WithTitle:@"拜访时间" withColor:GRAYCOLOR];
    [scrollView addSubview:timeLabel];
    
    UILabel *contactLabel = [self getLabelWith:CGRectMake(labX, timeLabel.maxY + labY , labW, height) WithTitle:@"联系人" withColor:GRAYCOLOR];
    [scrollView addSubview:contactLabel];
    UILabel *phoneLabel = [self getLabelWith:CGRectMake(labX, contactLabel.maxY + labY , labW, height) WithTitle:@"联系电话" withColor:GRAYCOLOR];
    [scrollView addSubview:phoneLabel];
    UILabel *recordLabel = [self getLabelWith:CGRectMake(labX, phoneLabel.maxY + labY , labW, height) WithTitle:@"拜访记录" withColor:GRAYCOLOR];
    [scrollView addSubview:recordLabel];
    
    
    _timeTF = [self addTextFieldWithFrame:CGRectMake(tfX, timeLabel.y-labY, tfW, labH) AndStr:@""];
    [scrollView addSubview:_timeTF];
    _timeTF.text = _recordModel.created_at;
    
    _nameTF = [self addTextFieldWithFrame:CGRectMake(tfX, contactLabel.y-labY, tfW, labH) AndStr:@""];
    [scrollView addSubview:_nameTF];
    _nameTF.text = _recordModel.contact;
    
    _telTF = [self addTextFieldWithFrame:CGRectMake(tfX, phoneLabel.y-labY, tfW, labH) AndStr:@""];
    _telTF.keyboardType = UIKeyboardTypeNumberPad;
    [scrollView addSubview:_telTF];
    _telTF.text = _recordModel.tel;
    
    
    _visitTV = [[UITextView alloc] initWithFrame:CGRectMake(tfX, recordLabel.y, tfW, labH*3)];
    _visitTV.font = [ViewTool getFont:15.0f];
    _visitTV.textColor = UIColorFromRGB(0x636365);
    _visitTV.layer.borderWidth = 0;
    _visitTV.contentInset = UIEdgeInsetsMake(-8.f, -5.f, -8.f, -5.f);
    
    _visitTV.text = _recordModel.desc;
    _visitTV.userInteractionEnabled = NO;
    CGSize titleSize = [_recordModel.desc boundingRectWithSize:CGSizeMake(tfW, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[ViewTool getFont:15.0f]} context:nil].size;
    
    _visitTV.height = titleSize.height+20;

    [scrollView addSubview:_visitTV];
    
    
    _picView = [[UIView alloc] initWithFrame:CGRectMake(tfX, _visitTV.maxY, tfW, picSize)];
    
    [scrollView addSubview:_picView];
    _visitPic1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, picSize, picSize)];
    _visitPic1.userInteractionEnabled = YES;
    [_picView addSubview:_visitPic1];
    
    _visitPic2 = [[UIImageView alloc] initWithFrame:CGRectMake(_visitPic1.maxX+10, _visitPic1.y, picSize, picSize)];
    _visitPic2.userInteractionEnabled = YES;
    [_picView addSubview:_visitPic2];
    
    _visitPic3 = [[UIImageView alloc] initWithFrame:CGRectMake(_visitPic2.maxX+10, _visitPic1.y, picSize, picSize)];
    _visitPic3.userInteractionEnabled = YES;
    [_picView addSubview:_visitPic3];
    
    [self setImages:_recordModel.images];
    
    UIView *line2 = [self getLineWith:CGRectMake(0, _picView.maxY + labY - 1, kScreenWidth, 1)];
    [scrollView addSubview:line2];
    
    bg.height = line2.maxY-grayH;
    
}

-(void)setImages:(NSArray *)imagesArray{
    if (imagesArray.count > 0) {
        [_visitPic1 sd_setImageWithURL:[NSURL URLWithString:imagesArray[0]] placeholderImage:nil];
        UITapGestureRecognizer *tap1  = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(magnifyImage1)];
        [_visitPic1 addGestureRecognizer:tap1];
        if (imagesArray.count > 1) {
            [_visitPic2 sd_setImageWithURL:[NSURL URLWithString:imagesArray[1]] placeholderImage:nil];
            UITapGestureRecognizer *tap2  = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(magnifyImage2)];
            [_visitPic2 addGestureRecognizer:tap2];
            
            if (imagesArray.count > 2) {
                [_visitPic3 sd_setImageWithURL:[NSURL URLWithString:imagesArray[2]] placeholderImage:nil];
                UITapGestureRecognizer *tap3  = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(magnifyImage3)];
                [_visitPic3 addGestureRecognizer:tap3];
            }else{
                [_visitPic3 sd_setImageWithURL:nil placeholderImage:nil];
            }
        }else{
            [_visitPic2 sd_setImageWithURL:nil placeholderImage:nil];
            [_visitPic3 sd_setImageWithURL:nil placeholderImage:nil];
        }
    }
    
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = NO;
}

- (UIImageView *)imageWith:(CGRect)frame withName:(NSString *)str{
    UIImageView *img = [[UIImageView alloc]initWithFrame:frame];
    img.image = [UIImage imageNamed:str];
    return img;
}
- (UILabel *)getLabelWith:(CGRect)frame WithTitle:(NSString *)title withColor:(UIColor *)color
{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.text = title;
    label.textColor = color;
    label.textAlignment = NSTextAlignmentLeft;
    label.font = [ViewTool getFont:15.0f];
    return label;
}
-(UIView *)getLineWith:(CGRect)frame
{
    UIView *view = [[UIView alloc] initWithFrame:frame];
    view.backgroundColor = UIColorFromRGB(0xe8e7ec);
    return view;
}

-(UITextField *)addTextFieldWithFrame:(CGRect)frame AndStr:(NSString *)placeholder
{
    UITextField *textF=[[UITextField alloc]initWithFrame:frame];
    textF.textColor = UIColorFromRGB(0x646368);
    textF.placeholder=placeholder;
    [textF setValue:UIColorFromRGB(0xe5e5e5) forKeyPath:@"_placeholderLabel.textColor"];
    textF.font = [ViewTool getFont:15.0f];
    [textF setValue:[ViewTool getFont:15.0f] forKeyPath:@"_placeholderLabel.font"];
    textF.userInteractionEnabled = NO;
    return textF;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

- (void)magnifyImage1
{
    NSLog(@"局部放大");
    if(!(_visitPic1.image == NULL)){
        [SJAvatarBrowser showImage:_visitPic1];//调用方法
    }
}
- (void)magnifyImage2
{
    NSLog(@"局部放大");
    if(!(_visitPic2.image == NULL)){
        [SJAvatarBrowser showImage:_visitPic2];//调用方法
    }
}
- (void)magnifyImage3
{
    NSLog(@"局部放大");
    if(!(_visitPic3.image == NULL)){
        [SJAvatarBrowser showImage:_visitPic3];//调用方法
    }
}

//- (void)magnifyImage1
//{
//    NSLog(@"局部放大");
//    ofSeeImageView *seeImageView = [[ofSeeImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
//    seeImageView.tag = 1210;
//    [seeImageView cresteScrollViewWithImages:_recordModel.images withPage:0];
//    [[UIApplication sharedApplication].keyWindow addSubview:seeImageView];
//    
//    if(!(_visitPic1.image == NULL)){
////        [SJAvatarBrowser showImage:_visitPic1];//调用方法
//    }
//}
//- (void)magnifyImage2
//{
//    NSLog(@"局部放大");
//    ofSeeImageView *seeImageView = [[ofSeeImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
//    seeImageView.tag = 1210;
//    [seeImageView cresteScrollViewWithImages:_recordModel.images withPage:1];
//    [[UIApplication sharedApplication].keyWindow addSubview:seeImageView];
//}
//- (void)magnifyImage3
//{
//    NSLog(@"局部放大");
//    ofSeeImageView *seeImageView = [[ofSeeImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
//    seeImageView.tag = 1210;
//    [seeImageView cresteScrollViewWithImages:_recordModel.images withPage:2];
//    [[UIApplication sharedApplication].keyWindow addSubview:seeImageView];
//}

@end
